package com.atguigu.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.hosp.repository.DepartmentRepository;
import com.atguigu.yygh.hosp.service.DeparmentService;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;

import com.atguigu.yygh.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DeparmentServiceImpl  implements DeparmentService {

    @Autowired
    private DepartmentRepository  departmentRepository;

    @Override
    public Boolean save(Map<String, Object> param) {
        String mapString = JSONObject.toJSONString(param);
        Department department = JSONObject.parseObject(mapString, Department.class);


        Department departmentTwo      =departmentRepository.getDepartmentByDepcodeAndHoscode(department.getDepcode(),department.getHoscode());

          if(departmentTwo!=null){

              departmentTwo.setUpdateTime(new Date());
              departmentTwo.setIsDeleted(0);

            departmentRepository.save(departmentTwo);



          }else{
              department.setCreateTime(new Date());
              department.setUpdateTime(new Date());
              department.setIsDeleted(0);
              departmentRepository.save(department);

          }


        return true;
    }

    @Override
    public Page<Department> findPageDeparment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
   //创建Pageable对象 设置当前页和每页记录数
        Pageable    pageable= PageRequest.of(page-1,limit);

       Department department=new Department();
        BeanUtils.copyProperties(departmentQueryVo,department);
        department.setIsDeleted(0);

   //创建 Example 对象
        ExampleMatcher  matcher=ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

       Example<Department> example=Example.of(department,matcher);

       Page<Department> all = departmentRepository.findAll(example, pageable);


        return all;
    }

    @Override
    public void remove(String hoscode, String depcode) {

       Department department   =departmentRepository.getByHoscodeAndDepcode( hoscode, depcode);
       if(department!=null){

           departmentRepository.deleteById(department.getId());
       }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
  //先准备一个视图对象
         List<DepartmentVo> result=new ArrayList<>();


         Department department=new Department();
           department.setHoscode(hoscode);

        Example e = Example.of(department);

 //更具hoscode查出所有的Department
        List<Department>  all     =  departmentRepository.findAll(e);

        Map<String, List<Department>> collect =
                all.stream().collect(Collectors.groupingBy(Department::getBigcode));

        for(Map.Entry<String,List<Department>> entry :collect.entrySet()){

            String bigcode = entry.getKey();
            List<Department> deparentList = entry.getValue();

            DepartmentVo departmentVo=new DepartmentVo();
            departmentVo.setDepcode(bigcode);
            departmentVo.setDepname(deparentList.get(0).getBigname());

            List<DepartmentVo>  children=new ArrayList<>();

            for(Department department1: deparentList){

                DepartmentVo  departmentVo2=new DepartmentVo();

                departmentVo2.setDepcode(department1.getDepcode());

                departmentVo2.setDepname(department1.getDepname());

                children.add(departmentVo2);
            }
            departmentVo.setChildren(children);

            result.add(departmentVo);

        }


        return result;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {


        Department byHoscodeAndDepcode = departmentRepository.getByHoscodeAndDepcode(hoscode, depcode);

        if(byHoscodeAndDepcode!=null){
            byHoscodeAndDepcode.getDepname();
        }
        return  null;
    }
}
